﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DGui;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RobotGame.ScreenManagers
{
    public class frmTwoView : DrawableGameComponent
    {
        DGuiManager _guiManager;
        GameTwoScreen game;

        DForm _form;

        DButton buttonPlay;
        DButton buttonPause;
        DButton buttonStop;

        DText taskLabel;
        DText parameterLabel;
        DText variableLabel;
        DText functionLabel;
        DText userfunctionLabel;
        DText attributeLabel;
        DText controlLabel;

        private DComboBox basicFunction;
        private DComboBox userFunction;
        private DComboBox attributeControl;
       
        DMultiLineTextBox task;

        DButton iconOneView;
        private DButton iconThreeView;

        private DLayoutFlow layout;
        public DComboBox BasicFunction
        {
            get { return basicFunction; }
            set { basicFunction = value; }
        }

        public DComboBox UserFunction
        {
            get { return userFunction; }
            set { userFunction = value; }
        }

        public DComboBox AttributeControl
        {
            get { return attributeControl; }
            set { attributeControl = value; }
        }

        public frmTwoView(GameTwoScreen game)
            : base(game.ScreenManager.Game)
        {
            this.game = game;
        }
     

        public void LoadContent(Viewport view)
        {

            _guiManager = new DGuiManager(game.ScreenManager.Game, game.ScreenManager.SpriteBatch);

            _form = new DForm(_guiManager, "GuiTest", null);
            _form.Size = new Vector2(view.Width, view.Height);
            _form.Position = new Vector2(view.X, view.Y);
            _form.Initialize();
            _guiManager.AddControl(_form);


            // Setup sample controls in a layout
            layout = new DLayoutFlow(1,60,view.Width, view.Height, DLayoutFlow.DLayoutFlowStyle.Vertically);
            layout.Position = new Vector2(0,0);
         
            #region "Fucntion"
            BasicFunction = new DComboBox(_guiManager);
            layout.Add(BasicFunction);
            BasicFunction.Position = new Vector2(565, 44);
            BasicFunction.Initialize();
            _form.AddPanel(BasicFunction);
            BasicFunction.OnChange+=new ComboBoxEventHandler(BasicFunction_OnChange);

            UserFunction = new DComboBox(_guiManager);
            layout.Add(UserFunction);
            UserFunction.Position = new Vector2(565, 197);
            UserFunction.Initialize();
            _form.AddPanel(UserFunction);
            UserFunction.OnChange += new ComboBoxEventHandler(UserFunction_OnChange);

            AttributeControl = new DComboBox(_guiManager);
            layout.Add(AttributeControl);
            AttributeControl.Position = new Vector2(340, 44);
            AttributeControl.Initialize();
            AttributeControl.AddItem("Test11", null);
            AttributeControl.AddItem("Test22", null);
            _form.AddPanel(AttributeControl);

            #endregion

            #region "Label"

            taskLabel = new DText(_guiManager);
            taskLabel.FontName = "Miramonte";
            layout.Add(taskLabel);
            taskLabel.Text = "Task";
            taskLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            taskLabel.Position = new Vector2(101,32);
            taskLabel.Initialize();
            _form.AddPanel(taskLabel);


            parameterLabel = new DText(_guiManager);
            parameterLabel.FontName = "Miramonte";
            layout.Add(parameterLabel);
            parameterLabel.Text = "Paramter";
            parameterLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            parameterLabel.Position = new Vector2(840, 32);
            parameterLabel.Initialize();
            _form.AddPanel(parameterLabel);

             variableLabel = new DText(_guiManager);
            variableLabel.FontName = "Miramonte";
            layout.Add(variableLabel);
            variableLabel.Text = "Variable";
            variableLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            variableLabel.Position = new Vector2(840, 187);
            variableLabel.Initialize();
            _form.AddPanel(variableLabel);

            
            functionLabel = new DText(_guiManager);
            functionLabel.FontName = "Miramonte";
            layout.Add(functionLabel);
            functionLabel.Text = "Basic Function";
            functionLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            functionLabel.Position = new Vector2(595, 32);
            functionLabel.Initialize();
            _form.AddPanel(functionLabel);

            userfunctionLabel = new DText(_guiManager);
            userfunctionLabel.FontName = "Miramonte";
            layout.Add(userfunctionLabel);
            userfunctionLabel.Text = "User Function";
            userfunctionLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            userfunctionLabel.Position = new Vector2(595, 187);
            userfunctionLabel.Initialize();
            _form.AddPanel(userfunctionLabel);

            attributeLabel = new DText(_guiManager);
            attributeLabel.FontName = "Miramonte";
            layout.Add(attributeLabel);
            attributeLabel.Text = "Attribute";
            attributeLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            attributeLabel.Position = new Vector2(350, 32);
            attributeLabel.Initialize();
            _form.AddPanel(attributeLabel);

            controlLabel = new DText(_guiManager);
            controlLabel.FontName = "Miramonte";
            layout.Add(controlLabel);
            controlLabel.Text = "Control";
            controlLabel.HorizontalAlignment = DText.DHorizontalAlignment.Left;
            controlLabel.Position = new Vector2(350, 187);
            controlLabel.Initialize();
            _form.AddPanel(controlLabel);

            #endregion

            #region "Button Control"
            buttonPlay = new DButton(_guiManager);
            layout.Add(buttonPlay);
            buttonPlay.Text = "Play";
            buttonPlay.Position = new Vector2(336, 212);
            buttonPlay.Initialize();
            _form.AddPanel(buttonPlay);
            buttonPlay.OnClick += new DButtonEventHandler(buttonPlay_OnClick);

            buttonPause = new DButton(_guiManager);
            layout.Add(buttonPause);
            buttonPause.Text = "Pause";
            buttonPause.Position = new Vector2(336, 254);
            buttonPause.Initialize();
            _form.AddPanel(buttonPause);

            buttonStop = new DButton(_guiManager);
            layout.Add(buttonStop);
            buttonStop.Text = "Stop";
            buttonStop.Position = new Vector2(336, 295);
            buttonStop.Initialize();
            _form.AddPanel(buttonStop);
            buttonStop.OnClick += new DButtonEventHandler(buttonStop_OnClick);
            #endregion

            #region "Task"
            task = new DMultiLineTextBox(_guiManager);
            task.Position = new Vector2(6,44);
            task.Size = new Vector2(244, 300);
            _form.AddPanel(task);
            #endregion


            iconOneView = new DButton(_guiManager);
            layout.Add(iconOneView);
            iconOneView.FontName = "MiramonteTiny";
            iconOneView.Text = ">>";
            iconOneView.Position = new Vector2(970, 3);
            iconOneView.Size = new Vector2(25,25);
            iconOneView.Initialize();
            _form.AddPanel(iconOneView);
            iconOneView.OnClick += new DButtonEventHandler(iconOneView_OnClick);


            iconThreeView = new DButton(_guiManager);
            layout.Add(iconThreeView);
            iconThreeView.FontName = "MiramonteTiny";
            iconThreeView.Text = "<<";
            iconThreeView.Position = new Vector2(940, 3);
            iconThreeView.Size = new Vector2(25, 25);
            iconThreeView.Initialize();
            _form.AddPanel(iconThreeView);
            iconThreeView.OnClick += new DButtonEventHandler(iconThreeView_OnClick);
        
        }

        void buttonStop_OnClick(GameTime gameTime)
        {
            game.RobotManager.ClearControl();
            task.Text = "";
        }

        void buttonPlay_OnClick(GameTime gameTime)
        {
            game.RobotManager.RunSchedule(0);
        }
        void UserFunction_OnChange(string value)
        {
            List<string> lbm = game.RobotManager.GetListMetaMethod(0);
            string methodname = null;
            foreach (string method in lbm)
            {
                string methods = method.Substring(method.IndexOf(" ") + 1);
                if (methods.Equals(value.Trim()))
                {
                    methodname = method;
                    break;
                }
            }
            AddControl(methodname);
        }
        void BasicFunction_OnChange(string value)
        {
            List<string> lbm = game.RobotManager.GetListBaseMethod(0);
            string methodname = null;
            foreach (string method in lbm)
            {
                string methods = method.Substring(method.IndexOf(" ") + 1);
                if (methods.Equals(value.Trim()))
                {
                    methodname = method;
                    break;
                }
            }
            AddControl(methodname);
            task.Text += methodname+"\n";
        }
        public void AddControl(string methodname)
        {
            if (methodname != null)
            {
                int idmethod = int.Parse(methodname.Split(' ')[0]);
                List<string> parameterInfos = game.RobotManager.GetListParam(0, idmethod);
                if (parameterInfos != null)
                {
                        RemoveControl("TextboxParamter");
                        RemoveControl("LabelParamter");
                        RemoveControl("ButtonParamter");

                        if (parameterInfos.Count == 0)
                        {
                            game.RobotManager.AddControl(0, idmethod, null);
                        }
                        for (int i = 0; i < parameterInfos.Count; i++)
                        {

                            string para = parameterInfos[i];
                           
                            DTextBox txtParameter1 = new DTextBox(_guiManager);
                            layout.Add(txtParameter1);
                            txtParameter1.Position = new Vector2(825, 44 + i * 25);
                            txtParameter1.Initialize();
                            txtParameter1.Tag = "TextboxParamter" + idmethod.ToString();
                            _form.AddPanel(txtParameter1);


                            DText par1Label = new DText(_guiManager);
                            layout.Add(par1Label);
                            par1Label.FontName = "Miramonte";
                            par1Label.Text = para;
                            par1Label.HorizontalAlignment = DText.DHorizontalAlignment.Left;
                            par1Label.Position = new Vector2(765, 63 + i * 25);
                            par1Label.Tag = "LabelParamter";
                            par1Label.Initialize();
                            _form.AddPanel(par1Label);
                            
                           
                        }
                        if (parameterInfos.Count > 0)
                        {
                            DButton btnOK = new DButton(_guiManager);
                            layout.Add(btnOK);
                            btnOK.Text = "OK";
                            btnOK.Position = new Vector2(825, 50 + parameterInfos.Count*25);
                            btnOK.Size = new Vector2(180, 28);
                            btnOK.Tag = "ButtonParamter" + idmethod.ToString();
                            btnOK.Initialize();
                            _form.AddPanel(btnOK);
                            btnOK.OnClick += new DButtonEventHandler(btnOK_OnClick);
                        }
                }
            }
        }

        void btnOK_OnClick(GameTime gameTime)
        {
            List<DPanel> listControl = FindControls("TextboxParamter");
            if(listControl.Count > 0)
            {
                DPanel control = listControl[0];
                string id = (string)control.Tag;
                string[] par = new string[] { "TextboxParamter" };
                int idmethod = int.Parse(id.Split(par, StringSplitOptions.RemoveEmptyEntries)[0]);
                List<object> parameters = new List<object>();

                for (int i = 0; i < listControl.Count; i++)
                {
                    DTextBox textBox = (DTextBox)listControl[i];
                    parameters.Add(textBox.Text);
                }
                int error = game.RobotManager.AddControl(0, idmethod, parameters);
                if (error == -1)
                {

                }
            }
            
        }
        private List<DPanel> FindControls(string name)
        {
            List<DPanel> listControl = new List<DPanel>();
            for (int i = 0; i < _form.Children.Count; i++)
            {
                DPanel control = (DPanel)_form.Children[i];
                if (control.Tag != null)
                {
                    string id = (string)control.Tag;
                    if (id.Contains(name))
                    {
                        listControl.Add(control);
                    }
                }
            }
            return listControl;
        }
        private DPanel FindControl(string name)
        {
            for (int i = 0; i < _form.Children.Count; i++)
            {
                DPanel control = (DPanel)_form.Children[i];
                if (control.Tag != null)
                {
                    string id = (string)control.Tag;
                    if (id.Contains(name))
                    {
                        return control;
                    }
                }
            }
            return null;
        }
        public void RemoveControl(string name)
        {
            for (int i = 0; i < _form.Children.Count; i++)
            {
                DPanel textBox = (DPanel)_form.Children[i];
                if (textBox.Tag != null)
                {
                    string id = (string)textBox.Tag;
                    if (id.Contains(name))
                    {
                        _form.Children.Remove(textBox);
                    }
                }
            }
        }
       
        void iconThreeView_OnClick(GameTime gameTime)
        {
            LoadingScreen.Load(game.ScreenManager, true, new GameplayScreen());
        }

        void iconOneView_OnClick(GameTime gameTime)
        {
            LoadingScreen.Load(game.ScreenManager, true, new GameOneScreen());
        }

        public override void Update(GameTime gameTime)
        {
            _guiManager.Update(gameTime);

            //base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _guiManager.Draw(gameTime);

            //base.Draw(gameTime);
        }
    }
}
